package logic;

import java.util.ArrayList;
import java.util.Arrays;

public class Constraint {
	private ArrayList<cell> cells = new ArrayList<cell>();
	
	public Constraint()
	{
		this.cells = new ArrayList<cell>();
	}
	
	public void addCell(cell cellToAdd)
	{
		this.cells.add(cellToAdd);
	}
	
	public ArrayList<cell> getCells()
	{
		return this.cells;
	}
	
	public boolean valueExistsWithinConstraint(int value)
	{
		for(cell cell:this.cells)
		{
			if(cell.getCurrentValue() == value)
			{
				return true;
			}
		}
		return false;
	}
	
	/**
	 * test if this constraint can become satisfied in its current state (no repeated values) 
	 * @return true if repeated number exist false other wise
	 */
	public Boolean violated()
	{
		Boolean[] unique = new Boolean[cells.size()+1];
		Arrays.fill(unique,Boolean.FALSE);
		int testvalue;
		for(int i = 0;i < 9; i++)
		{
			testvalue = cells.get(i).getCurrentValue();
			if(testvalue != cell.BLANK)
			{
				if( unique[testvalue] == true)
				{
					return true;
				}
				else
				{
					unique[testvalue] = true;
				}
			}
		}
		return false;
	}
	
	/**
	 * tests if this constraint is satisfied (unique numbers 1 - 9)
	 * @return true if satisfied false other wise
	 */
	public Boolean satisfied()
	{
		Boolean[] unique = new Boolean[cells.size()+1];
		Arrays.fill(unique,Boolean.FALSE);
		int testvalue;
		for(int i = 0;i < 9; i++)
		{
			testvalue = cells.get(i).getCurrentValue();
			if(testvalue > 9 || unique[testvalue] == true)
			{
				return false;
			}
			else
			{
				unique[testvalue] = true;
			}
		}
		
		for(int i = 1;i < 10;i++)
		{
			if(unique[i] == false)
			{
				return false;
			}
		}
		return true;
	}
}
